package com.sgb.knowledgeshare.view.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;
import com.github.jdsjlzx.ItemDecoration.GridItemDecoration;
import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.sgb.knowledgeshare.R;
import com.sgb.knowledgeshare.databinding.ActivityShareVideoEditBinding;
import com.sgb.knowledgeshare.event.shareLeaseEvent;
import com.sgb.knowledgeshare.view.adapter.UserVideoEditAdaoter;
import com.sgb.knowledgeshare.viewmodel.ShareUserVideoEditModel;
import com.swgk.core.base.AppActivity;
import com.swgk.core.base.model.entity.VideoBean;
import com.swgk.core.event.EventConstant;
import com.swgk.core.dialog.RoundCornerDialog;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;
import org.greenrobot.eventbus.EventBus;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by fq on 2020/9/25 0025
 * Description： 分享-个人中心-用户视频编辑
 */
public class ShareUserVideoEditActivity extends AppActivity {

    ShareUserVideoEditModel videoModel;
    private ActivityShareVideoEditBinding binding;
    private String userNo;
    private int page;
    private boolean isLoadMore;
    private LRecyclerViewAdapter mLRecyclerViewAdapter;
    private UserVideoEditAdaoter uservideoeditadaoter;
    private List<VideoBean> listvideo;
    private List<Long> delectList;
    private int  DELETEVIDEO = 2;

    public static void start(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, ShareUserVideoEditActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().postSticky(this);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_share_video_edit);
        videoModel = new ViewModelProvider(this).get(ShareUserVideoEditModel.class);
        videoModel.setActivity(ShareUserVideoEditActivity.this);
        View decor = getWindow().getDecorView();
        decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        binding.include.ivBack.setOnClickListener(this);
        binding.include.titleView.setText("删除视频");
        binding.userVideoDelete.setOnClickListener(this);
        binding.userVideoCancel.setOnClickListener(this);
        userNo = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.USER_NO, "");
        uservideoeditadaoter = new UserVideoEditAdaoter(this, null);
        mLRecyclerViewAdapter = new LRecyclerViewAdapter(uservideoeditadaoter);
        com.github.jdsjlzx.ItemDecoration.GridItemDecoration divider = new GridItemDecoration.Builder(this)
                .setHorizontal(R.dimen.dp3)
                .setVertical(R.dimen.dp3)
                .setColorResource(R.color.white)
                .build();
        binding.lrecyclerview.addItemDecoration(divider);
        GridLayoutManager layoutManager = new GridLayoutManager(this, 3);
        binding.lrecyclerview.setLayoutManager(layoutManager);
        binding.lrecyclerview.setAdapter(mLRecyclerViewAdapter);
        binding.lrecyclerview.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                uservideoeditadaoter.clearDatas();
                mLRecyclerViewAdapter.notifyDataSetChanged();
                initData();
            }
        });
        binding.lrecyclerview.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                isLoadMore = true;
                page++;
                videoModel.getVideoListData(page, userNo);
            }
        });
        uservideoeditadaoter.setOnvideolistbean(new UserVideoEditAdaoter.OnVideoListBean() {
            @Override
            public void onClickBean(String videoId) {
                for (int i = 0; i < listvideo.size(); i++) {
                    if (listvideo.get(i).getVideoId().equals(videoId)) {
                        listvideo.get(i).setSelect(!listvideo.get(i).isSelect());
                    }
                }
                mLRecyclerViewAdapter.notifyDataSetChanged();
            }
        });
        mLRecyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
             //   ShareUserVideoInfoActivity.start(getApplicationContext(), listvideo.get(position));
            }
        });
    }

    private void initData() {
        isLoadMore = false;
        page = 1;
        videoModel.getVideoListData(page, userNo);
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.user_video_cancel) {//取消
            finish();
        } else if (id == R.id.user_video_delete) {//删除
            AssembleData();//组装需要删除的数据
        } else if (id == R.id.iv_back) {//返回
            finish();
        }
    }

    //组装需要删除的数据
    private void AssembleData() {
        delectList = new ArrayList<>();
        for (VideoBean beans : listvideo) {
            if (beans.isSelect() == true) {
                delectList.add((long) beans.getId());
            }
        }
        if (delectList.size() > 0) {
            videoDeleteDialog(delectList);
        } else {
            MToast.showLongToast(this, "请选择需要删除的视频！");
        }
    }


    private void videoDeleteDialog(List<Long> delectList) {
        View view = View.inflate(getActivity(), R.layout.dialog_two_btn, null);
        final RoundCornerDialog roundCornerDialog = new RoundCornerDialog(getActivity(), 0, 0, view, R.style.RoundCornerDialog);
        roundCornerDialog.show();
        roundCornerDialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失
        TextView tv_message = view.findViewById(R.id.tv_message);
        TextView tv_logout_confirm = view.findViewById(R.id.tv_logout_confirm);
        TextView tv_logout_cancel = view.findViewById(R.id.tv_logout_cancel);
        tv_message.setText("您的作品将被永久删除，无法找回。确认删除？");
        //确定
        tv_logout_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //删除视频的操作逻辑
                roundCornerDialog.dismiss();
                videoModel.deleteMyVideo(delectList);
            }
        });
        //取消
        tv_logout_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                roundCornerDialog.dismiss();
            }
        });
    }

    public void setVideoListData(List<VideoBean> records) {
        if (records == null) return;
        for (VideoBean bean : records) {
            bean.setSelect(false);
        }
        binding.lrecyclerview.refreshComplete(page);
        if (isLoadMore) {
            if (records.size() > 0) {
                uservideoeditadaoter.addDatas(records);
            } else {
                MToast.showToast(getApplicationContext(), "没有视频啦");
            }
        } else {
            if (records.size() > 0) {
                binding.lrecyclerview.setVisibility(View.VISIBLE);
                binding.empty.setVisibility(View.GONE);
                uservideoeditadaoter.setDatas(records);
            } else {
                binding.lrecyclerview.setVisibility(View.GONE);
                binding.empty.setVisibility(View.VISIBLE);
                uservideoeditadaoter.setDatas(null);
            }
        }
        this.listvideo = uservideoeditadaoter.getDatas();
    }

    public void VideoInfoList() {
        MToast.showToast(getActivity(), "删除视频成功！");
        finish();
        EventBus.getDefault().postSticky(shareLeaseEvent.make().setMessageTag(EventConstant.EVENT_DELECT_VIDEO).setMessageData(DELETEVIDEO));
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
